import java.util.HashMap;

public class T1有效括号匹配 {
    //就是按照顺序（左-右（匹配、不匹配））
    public boolean isValid (String s) {
        if(s.length() == 0) return true;

        char[] ss = s.toCharArray();

        StringBuilder sb = new StringBuilder();//模拟栈

        HashMap<Character,Character> map = new HashMap<>();
        map.put(']','[');
        map.put('}','{');
        map.put(')','(');

        for(char x : ss) {
            if (map.containsValue(x)) {
                //左括号
                sb.append(x);
            } else {
                //右括号
                //匹配
                if (sb.length() > 0 && map.get(x) == sb.charAt(sb.length()-1)) {
                    sb.deleteCharAt(sb.length()-1);
                } else {
                    //匹配
                    return false;
                }
            }
        }
        return sb.length() == 0;
    }

    public static void main(String[] args) {

    }
}
